package P125_PriorityThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author xufy
 * @version 1.0
 * @description 优先选择线程池
 **/
public class PriorityThreadPool {

    //线程都有启动、运行、销毁过程，一个线程不能重复使用
    public static void main(String[] args) throws InterruptedException {
        //new thread创建线程
        thread1();
        //使用线程池
        thread2();


    }

    public static void thread1() throws InterruptedException {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程在运行。。。。");
            }
        });
        t.start();
        while (!t.getState().equals(Thread.State.TERMINATED)) {
            TimeUnit.MILLISECONDS.sleep(10);
        }
        t.start();
    }

    public static void thread2() {
        ExecutorService es = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 4; i++) {
            es.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });

        }
        es.shutdown();
    }
}

         /**
          * corePoolSize:核心线程数
          * maximumPoolSize：线程池所容纳最大线程数(workQueue队列满了之后才开启)
          * keepAliveTime：非核心线程闲置时间超时时长
          * unit：keepAliveTime的单位
          * workQueue：等待队列，存储还未执行的任务
          * threadFactory：线程创建的工厂
          * handler：异常处理机制
          *
          */
//        new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,
//                    KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue<>(QUEUE_LENGTH),
//        threadFactory, new ThreadPoolExecutor.AbortPolicy());